Choose = class()

function Choose:init()
    self.nColor = color(0, 0, 0, 0)
    self.kb = KeyBoard(function(key)  end)
    self.showing = false
    self.yOffset = HEIGHT
    self.ny = HEIGHT
    self.showNew = false
    
    self.back = STButton(60,HEIGHT - 50,LP.back,"Project:RB","Project:B")
    self.back.width = 100
    self.back.height = 60
    self.back.color = color(255, 255, 255, 255)
    self.back.pressedColor = color(136, 135, 135, 255)
    self.back.clicked = 
    function()
        tween(0.5,self,{yOffset = HEIGHT},tween.easing.cubicOut,function() self.showing = false end)
    end
    
    self.add = STButton(WIDTH-60,HEIGHT-50,LP.newGame,"Project:GB","Project:B")
    self.add.width = 100
    self.add.height = 60
    self.add.color = color(255, 255, 255, 255)
    self.add.pressedColor = color(136, 135, 135, 255)
    self.add.clicked = 
    function() 
        self:newSaveChoose() 
        self.kb:clearBuffer() 
    end
    
    self.ok = STButton(WIDTH/2+200,HEIGHT/2-300,LP.ok,"Project:BB","Project:B")
    self.ok.width = 350
    self.ok.color = color(255, 255, 255, 255)
    self.ok.pressedColor = color(136, 135, 135, 255)
    self.ok.clicked = 
    function() 
        local lc = 1
        local l
        for i = 1,self.listCount do
            l = tonumber(string.sub(self.list[i],string.len(DefaultFileName) + 1))
            if l and l + 1 > lc then
                lc = l + 1
            end
        end
        lc = lc or self.listCount
        self.yOffset = HEIGHT
        self.showing = false
        if self.kb:getBuffer() ~= "" or self.kb:getBuffer() ~= nil then
            game:newGame(self.kb:getBuffer())
        else
            game:newGame(DefaultFileName..lc)
        end
        tween(0.5,self,{ny = HEIGHT},tween.easing.cubicOut,function() self.showNew = false end)
        tween(0.5,self.nColor,{a = 0},tween.easing.cubicOut)
    end
    
    self.canecl = STButton(WIDTH/2-200,HEIGHT/2-300,LP.cancel,"Project:BB","Project:B")
    self.canecl.width = 350
    self.canecl.color = color(255, 255, 255, 255)
    self.canecl.pressedColor = color(136, 135, 135, 255)
    self.canecl.clicked =
    function()
        tween(0.5,self,{ny = HEIGHT},tween.easing.cubicOut,function() self.showNew = false end)
        tween(0.5,self.nColor,{a = 0},tween.easing.cubicOut)
    end
    
end

function Choose:newSaveChoose()
    self.showNew = true
    self.ny = HEIGHT
    tween(0.5,self,{ny = 0},tween.easing.cubicOut)
    tween(0.5,self.nColor,{a = 190},tween.easing.cubicOut)
end

function Choose:updateList()
    self.list = game.world:getSaveList()
    self.listCount = #self.list 
    return false
end

function Choose:choice()
    if self:updateList() then return end
    self.showing = true
    self.yOffset = HEIGHT
    tween(0.5,self,{yOffset = 0},tween.easing.cubicOut)
end

function Choose:draw()
    pushMatrix()
    pushStyle()
    translate(0,self.yOffset)
    self.back:draw() 
    if self.listCount < 12 then
        self.add:draw()
    end 
    fill(255, 255, 255, 255)
    fontSize(40)
    textMode(CENTER)
    text(LP.list,WIDTH/2,HEIGHT - 50)
    for i = 1,self.listCount do
        pushStyle()
        rectMode(CENTER)
        fill(185, 185, 185, 255)
        sprite("Project:BB",WIDTH * 0.5,HEIGHT-100-i*65,300,60)
        fill(0, 0, 0, 255)
        text(self.list[i],WIDTH * 0.5,HEIGHT-100-i*65)
        noStroke()
        fill(255, 58, 0, 255)
        spriteMode(CENTER)
        sprite("Project:RB",WIDTH * 0.75,HEIGHT-100-i*65,100,60)
        fontSize(25)
        fill(255, 255, 255, 255)
        text(LP.delete,WIDTH * 0.75,HEIGHT-100-i*65)
        popStyle()
    end
    popStyle()
    popMatrix()
     
    pushStyle()
    fill(self.nColor)
    rect(-200,-200,WIDTH+400,HEIGHT+400)
    popStyle()
    
    pushMatrix()
    pushStyle()
    fontSize(40)
    translate(0,self.ny)
    rectMode(CENTER)
    textMode(CENTER)
    fill(255, 255, 255, 255)
    rect(WIDTH/2,HEIGHT/2,800,700)
    fill(5, 5, 5, 255)
    text(LP.newGame,WIDTH/2,HEIGHT/2+300)
    
    pushStyle()
    textMode(CORNER)
    rectMode(CORNER)
    fill(82, 82, 82, 255)
    rect(WIDTH/2-220,HEIGHT/2+200,600,43)
    fill(0, 0, 0, 255)
    text(LP.names,WIDTH/2-360,HEIGHT/2+200)
    fill(255, 255, 255, 255) 
    text(self.kb:getBuffer(),WIDTH/2-210,HEIGHT/2+200)    
    popStyle()
      
    self.ok:draw()
    self.canecl:draw()
    
    popStyle()
    popMatrix()            
end

function Choose:touched(touch)
    self.back:touched(touch)
    if self.listCount < 12 then
        self.add:touched(touch)
    end
    if  self.showNew == false then
        for j = 1,self.listCount do
            if self:touchInPos(touch,j) and touch.state == ENDED then
                -- 很随意的写法，以后必定出bug= =，比如存档名太长时
                if touch.x > WIDTH * 0.7 then
                    
                    saveText("Project:"..self.list[j],"")
                    self:updateList()
                else
                    self.yOffset = HEIGHT
                    self.showing = false
                    game:readGame(self.list[j])
                end
            end
        end
    else
        self.canecl:touched(touch)
        self.ok:touched(touch)
        if touch.y > HEIGHT/2+200 then
            self.kb:show()
        else self.kb:hide()
        end
    end
    
end

function Choose:touchInPos(touch,point)
    return touch.x > math.abs((WIDTH/2) - 300) and touch.x<math.abs((WIDTH/2) + 300) and touch.y > math.abs(((HEIGHT - 100) - 59 * point) - 20) and touch.y < math.abs(((HEIGHT - 100) - 59 * point) + 20)
end

